// pages/socialSystem/myArtical/myArtical.js
const common = require('../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        articalList: [],
        nowPage: 0,
        isLoading: false,
        isAll: false,
        videoPlayIndex: null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        this.getMyArtical()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        this.getMyArtical()
    },

    getMyArtical: function() {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        const that = this
        let articalList = this.data.articalList
        let data = {
            limit: 10,
            order_by: 'add_time',
            user_id: wx.getStorageSync('userId'),
            page: this.data.nowPage
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中',
        })
        common.post('Moments/moment_list', data).then(function(res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    articalList: articalList,
                    isAll: true
                })
            } else {
                articalList = articalList.concat(res)
                that.setData({
                    articalList: articalList,
                    nowPage: that.data.nowPage + 1
                })
            }
        }).catch(function(e) {})
    },

    goArtical: function(e) {
        wx.navigateTo({
            url: '/pages/communitySystem/detail/detail?id=' + e.currentTarget.dataset.id
        })
    },

    goDelete: function(e) {
        const that = this
        wx.showModal({
            title: '提示',
            content: '确认删除？',
            success(res) {
                if (res.confirm) {
                    let data = {
                        moments_id: e.currentTarget.dataset.id
                    }
                    common.post('Moments/moment_delete', data).then(function(res) {
                        wx.showToast({
                            title: '删除成功',
                            icon: 'none',
                            duration: 2000
                        })
                        that.setData({
                            articalList: [],
                            nowPage: 0,
                            isLoading: false,
                            isAll: false
                        })
                        that.getMyArtical()
                    }).catch(function(e) {})
                }
            }
        })
    },

    donothing: function () {
        return
    },
    
    videoPlay: function(e) {
        if (this.data.videoPlayIndex == null || this.data.videoPlayIndex == e.currentTarget.dataset.index) {
            this.setData({
                videoPlayIndex: e.currentTarget.dataset.index
            })
        } else {
            let videoContextPrev = wx.createVideoContext('video' + this.data.videoPlayIndex)
            videoContextPrev.pause()
            this.setData({
                videoPlayIndex: e.currentTarget.dataset.index
            })
        }
    }
})